package com.mangatta.ec2.adaptor;

import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.mangatta.cloud.ec2.Ec2Instance;
import com.mangatta.ec2.activity.R;

import java.util.List;

public class InstanceListAdaptor extends ArrayAdapter<Ec2Instance> {
    private final static String TAG = InstanceListAdaptor.class.getSimpleName();

    private final Activity activity;
    private List<Ec2Instance> ec2Instances;

    public InstanceListAdaptor(Activity activity, List<Ec2Instance> ec2Instances) {
        super(activity, R.layout.instance_row, ec2Instances);
        this.activity = activity;
        this.ec2Instances = ec2Instances;
    }

    @Override
    public View getView(int position, View rowView, ViewGroup parent) {
        InstanceView instanceView;

        if (rowView == null) {
            LayoutInflater layoutInflater = activity.getLayoutInflater();
            rowView = layoutInflater.inflate(R.layout.instance_row, null);
            instanceView = new InstanceView();
            instanceView.instanceId = (TextView) rowView.findViewById(R.id.instance_id);
            instanceView.alarmsConfigured = (TextView) rowView.findViewById(R.id.alarm_names);
            Log.d(TAG, "View " + instanceView);
            rowView.setTag(instanceView);
        } else {
            instanceView = (InstanceView) rowView.getTag();
        }

        Ec2Instance ec2Instance = ec2Instances.get(position);
        instanceView.instanceId.setText(ec2Instance.getInstanceId());
        // Maybe, we can show the active/inactive state of the instance here .
        instanceView.alarmsConfigured.setText("Click here to configure alarms");
        return rowView;
    }

    public void setEc2Instances(List<Ec2Instance> ec2Instances) {
        this.ec2Instances = ec2Instances;
        notifyDataSetChanged();
    }


}
